<?php

Class CocktailsController extends AppController {

    public $name = 'Cocktails';

    function beforeFilter() {
        $this->Auth->allow('search', 'select');
    }

    function admin_edit($id) {
        $this->layout = "registered";
        $this->Cocktail->recursive = -1;
        if (empty($this->data)) {
            $this->data = $this->Cocktail->find('first', array('conditions' => array('Cocktail.id' => $id), 'fields' => array('Cocktail.id', 'Cocktail.name', 'Cocktail.description', 'Cocktail.image', 'Cocktail.id')));
        } else {
            $this->Session->setFlash('entraaaaa', 'flash_custom');

            $rutaArchivo = APP . "webroot" . "/" . "img" . "/" . "cocktails";
            $nombreArchivo = $id . ".jpg";
            $rutaArchivo.= "/" . $nombreArchivo;

            if (move_uploaded_file($this->data['Cocktail']['file']['tmp_name'], $rutaArchivo)) {
                echo "Se ha cargado la imagen correctamente  ";
            } else {
                echo "Error cargando imagen";
            };

            $this->data['Cocktail']['image'] = $nombreArchivo;

            if ($this->Cocktail->save($this->data)) {
                $this->Session->setFlash('Registro editado correctamente', 'flash_custom');
                $this->redirect(array('controller' => 'Cocktails', 'action' => 'index'));
            } else {
                $this->Session->setFlash('Datenfehler', 'flash_custom');
            }
        }
    }

    function admin_index() {
        $this->layout = "registered";
        $this->paginate = array('Cocktail' => array('recursive' => -1, 'maxLimit' => 500, 'limit' => 10, 'fields' => array('Cocktail.id', 'Cocktail.name', 'Cocktail.description', 'Cocktail.image')));
        $this->set('Cocktails', $this->paginate());
    }

    function public_index() {
        $this->layout = "ajax";
        $this->paginate = array('Cocktail' => array('recursive' => -1, 'maxLimit' => 500, 'limit' => 10, 'fields' => array('Cocktail.id', 'Cocktail.name', 'Cocktail.description', 'Cocktail.image')));
        $this->set('Cocktails', $this->paginate());
    }

    function admin_add() {
        $this->layout = "registered";
        if (empty($this->data)) {
            
        } else {
            if ($this->Cocktail->save($this->data)) {
                $id = $this->Cocktail->getInsertId();
                $rutaArchivo = APP . "webroot" . "/" . "img" . "/" . "cocktails";
                $nombreArchivo = $id . ".jpg";
                $rutaArchivo.= "/" . $nombreArchivo;



                if (move_uploaded_file($this->data['Cocktail']['file']['tmp_name'], $rutaArchivo)) {
                    echo "Se ha cargado la imagen correctamente  ";
                    $this->Cocktail->saveField('image', $rutaArchivo, array('Coctail.id' => $id));
                } else {
                    echo "Error cargando imagen";
                };


                $this->Session->setFlash('Registro Adicionado correctamente', 'flash_custom');
                $this->redirect(array('controller' => 'Cocktails', 'action' => 'index'));
            } else {
                $this->Session->setFlash('Datenfehler');
            }
        }
    }

    public function search() {
        $this->layout = "index_default";
        //Busco todos los cocteles que haya en la base de datos.
        $this->Cocktail->recursive = -1;
        $this->set('cocktails', $this->Cocktail->find('list', array('order' => 'Cocktail.name ASC', 'fields' => array('Cocktail.id', 'Cocktail.name'))));
    }

    public function select() {
        $this->Cocktail->recursive = -1;
        $this->set('cocktail', $this->Cocktail->find('first', array(
                    'conditions' => array('Cocktail.id' => $this->data['Cocktail']['id']),
                    'fields' => array('Cocktail.*')
                        )
                ));
        $this->set('ingredients', $this->Cocktail->CocktailIngredient->find('all', array(
                    'conditions' => array('Cocktail.id' => $this->data['Cocktail']['id']),
                    'fields' => array('Ingredient.name', 'CocktailIngredient.size', 'CocktailIngredient.unit')
                        )
                ));
    }

    function delete($id) {
        if ($this->Cocktail->delete($id, true)) {
            $this->Session->setFlash(_("Cocktail eliminado con éxito"), "flash_custom");
            $this->redirect(array('controller' => 'Cocktails', 'action' => 'index'));
        }
    }

}

?>